//
//  F_BankCardListVC.swift
//  letuofu
//
//  Created by WH-JS on 2020/3/13.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit

class F_BankCardListVC: BaseViewControllerWithHeader,UITableViewDataSource,UITableViewDelegate {
    
    let tableView = UITableView(frame: CGRect.zero, style: .plain)
    var dataList = [[String:Any?]]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        reloadData()
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

    func getBankCardList(callback: @escaping ([String:Any?]) -> Void) {
        let params :[String:Any] = ["identityToken":LocalStorage.getString(forKey: USER_IDENTITYTOKEN) ?? ""]
        API.request(url: .F_GetBankCardList, params: params) { (result) in
            if result.success {
               if let dl = result.data?.getDictionary("data") {
                callback(dl)
               }
            }else{
            }
        }
    }
}

// MARK: UI
extension F_BankCardListVC {
    func setupUI() {
        safeView.backgroundColor = .xF2F2F2
        headView.backgroundColor = .white
        headTitle.text = "借记卡"
        
        safeView.addSubview(tableView)
        tableView.backgroundColor = UIColor.clear;
        tableView.register(F_BankCardCell.self, forCellReuseIdentifier: "F_BankCardCell")
        tableView.separatorStyle = .none
        tableView.dataSource = self
        tableView.delegate = self
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
}


// MARK: 事件
extension F_BankCardListVC {
    func reloadData() {
        getBankCardList(callback: { (data) in
            self.dataList = data.getDictList("items") ?? []
            self.tableView.reloadData()
        })
    }
    
    @objc func _submiButtonClick(button: Button){
        let vc = F_AddBankCardVC()
        pushViewController(vc)
    }
    
    ///MARK: UITableViewDelegate
    // 选中cell后执行此方法
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    
    ///MARK: UITableViewDataSource
    
    //头部高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
       return 0
    }
    
    //组数
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    //尾部高度
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 100
    }
    
    //尾部view
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footView = UIView()
        footView.backgroundColor = .clear
        let btn = footView.drawButton(title: "  添加借记卡", titleColor: .white, fontSize: 15) { (make) in
            make.center.equalToSuperview()
            make.left.equalToSuperview().offset(90)
            make.right.equalToSuperview().offset(-90)
            make.height.equalTo(35)
        }
        btn.backgroundColor = .xFC4C02
        btn.layer.cornerRadius = 7
        btn.addTarget(self, action: #selector(_submiButtonClick(button:)), for: .touchUpInside)
        
        btn.drawImage(named: "add_icon") { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(43)
        }
        return footView
    }
    
    // 设置cell高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 110
    }
    
    // cell的个数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    
    // UITableViewCell
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cellid = "F_BankCardCell"
            var cell = tableView.dequeueReusableCell(withIdentifier: cellid) as? F_BankCardCell
            if cell==nil {
                cell = F_BankCardCell(style: .default, reuseIdentifier: cellid)
            }
            
            cell?.data = dataList[indexPath.row]
            return cell!
    }
}
